import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'device_scanner_controller.dart';
import 'scan_frame_painter.dart';
import 'scanner_error_widget.dart';

class DeviceScannerPage extends GetView<DeviceScannerController> {
  const DeviceScannerPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DeviceScannerController>(
      builder: (controller) => Stack(
        children: [
          Visibility(visible:!controller.cameraIsDenied,child: MobileScanner(
            // startDelay: false,
            controller: controller.scanController,
            fit: BoxFit.fitHeight,
            // onScannerStarted: (MobileScannerArguments? arguments) {
            //   Future.delayed(const Duration(milliseconds: 100)).then((value){
            //     controller.backgroundColor = Colors.transparent;
            //     controller.update();
            //   });
            // },
            // errorBuilder: (context,MobileScannerException error, child) {
            //   if(error.errorCode == MobileScannerErrorCode.permissionDenied){
            //     Log.d('扫描失败,请返回后重试==' + error.errorCode.toString());
            //     // return;
            //   }
            //   Log.d('扫描失败,请返回后重试==' + error.errorCode.toString());
            //   Log.d('扫描失败,请返回后重试==' + error.errorDetails.toString());
            //   // if(){
            //   //
            //   // }
            //   // Get.toNamed(RouterPath.deviceScanFail, arguments: {
            //   //   'siteId': controller.siteId,
            //   //   'message': '扫描失败,请返回后重试'
            //   // });
            //   return  const SizedBox();
            // },
            onDetect: (barcode) {
              // print('扫描成功  $barcode');
              if (controller.isLoading == false) {
                var data = barcode.barcodes[0].rawValue;
                if (data != null) {
                  controller.getDevice(data);
                } else {
                  Log.d('扫描失败,请返回后重试==11111');
                  Get.toNamed(RouterPath.deviceScanFail, arguments: {
                    'siteId': controller.siteId,
                    'message': '扫描失败,请返回后重试'
                  });
                }
              }
            },
          ))
          ,
          Positioned(
            left: 0,
            right: 0,
            top: 0,
            bottom: 0,
            child: DecoratedBox(
              decoration: BoxDecoration(
                color: const Color(0xFF151823).withOpacity(0.3),
              ),
            ),
          ),
          Scaffold(
            backgroundColor: Colors.transparent,
            appBar: AppBar(
              leading: GestureDetector(
                onTap: () => Get.back(),
                child: Icon(
                  Platform.isAndroid ? Icons.arrow_back : Icons.arrow_back_ios,
                  color: Colour.white,
                ),
              ),
              title: Text(
                ResKey.scanDevices.tr,
                style: TextStyle(
                    color: Colour.white,
                    fontSize: 18.sp,
                    fontWeight: FontWeight.w500),
              ),
              backgroundColor: Colors.transparent,
            ),
            body: Column(
              children: [
                SizedBox(
                  height: 40.h,
                ),
                Visibility(
                  visible: controller.scanFail,
                  child: Text(
                    ResKey.scanFailTip.tr,
                    style: TextStyles.styleWith(16.sp, color: Colour.white),
                  ),
                ),
                SizedBox(
                  height: 10.h,
                ),
                Container(
                  margin: EdgeInsets.only(left: 16.w,right: 16.w),
                  child: Text(
                  '${ResKey.scanTheQR.tr} ${ResKey.manualAddition.tr}',
                  style: TextStyles.lf14,
                ),),
                // Padding(
                //   padding: EdgeInsets.symmetric(
                //     vertical: 5.h,
                //   ),
                //   child: Text(
                //     '"${ResKey.manualAddition.tr}"',
                //     style: TextStyles.lf14,
                //   ),
                // ),
                CustomPaint(
                  painter: ScanFramePainter(
                      lineMoveValue: controller.animation.value),
                  child: Container(
                    height: 300.h,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
